package com.flnet.web.controller.foxwash;

import com.flnet.common.annotation.Log;
import com.flnet.common.base.AjaxResult;
import com.flnet.common.enums.BusinessType;
import com.flnet.common.page.TableDataInfo;
import com.flnet.common.utils.UUIDUtils;
import com.flnet.common.utils.poi.ExcelUtil;
import com.flnet.foxwash.domain.WServiceItem;
import com.flnet.foxwash.domain.WServiceType;
import com.flnet.foxwash.domain.WShop;
import com.flnet.foxwash.domain.WShopPrice;
import com.flnet.foxwash.service.IWServiceItemService;
import com.flnet.foxwash.service.IWServiceTypeService;
import com.flnet.foxwash.service.IWShopPriceService;
import com.flnet.foxwash.service.IWShopService;
import com.flnet.framework.util.ShiroUtils;
import com.flnet.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务价格设定 信息操作处理
 * 
 * @author flnet
 * @date 2019-03-20
 */
@Controller
@RequestMapping("/foxwash/wShopPrice")
public class WShopPriceController extends BaseController
{
    private String prefix = "foxwash/wShopPrice";
	
	@Autowired
	private IWShopPriceService wShopPriceService;

	@Autowired
	private IWShopService wShopService;

	@Autowired
	private IWServiceTypeService wServiceTypeService;

	@Autowired
	private IWServiceItemService wServiceItemService;

	@RequiresPermissions("foxwash:wShopPrice:view")
	@GetMapping()
	public String wShopPrice(ModelMap mmap)
	{
		mmap.put("shopList",wShopService.selectWShopList(null));
		mmap.put("serviceTypeList",wServiceTypeService.selectWServiceTypeList(null));
		mmap.put("serviceItemList",wServiceItemService.selectWServiceItemList(null));
	    return prefix + "/wShopPrice";
	}
	
	/**
	 * 查询服务价格设定列表
	 */
	@RequiresPermissions("foxwash:wShopPrice:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WShopPrice wShopPrice)
	{
		startPage();
        List<WShopPrice> list = wShopPriceService.selectWShopPriceList(wShopPrice);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出服务价格设定列表
	 */
	@RequiresPermissions("foxwash:wShopPrice:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WShopPrice wShopPrice)
    {
    	List<WShopPrice> list = wShopPriceService.selectWShopPriceList(wShopPrice);
        ExcelUtil<WShopPrice> util = new ExcelUtil<WShopPrice>(WShopPrice.class);
        return util.exportExcel(list, "wShopPrice");
    }
	
	/**
	 * 新增服务价格设定
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{

		mmap.addAttribute("typeList",wServiceTypeService.selectWServiceTypeList(null));
		mmap.addAttribute("itemList",wServiceItemService.selectWServiceItemList(null));
		mmap.addAttribute("shopList",wShopService.selectWShopList(null));
	    return prefix + "/add";
	}

	@ResponseBody
	@RequestMapping("/updateItem")
	public AjaxResult updateItem(int typeId){
		WServiceItem item = new WServiceItem();
		item.setTypeId((long)typeId);
		List<WServiceItem> itemList = wServiceItemService.selectWServiceItemList(item);
		List<WServiceItem> items = new ArrayList<WServiceItem>();
		for(WServiceItem ItemVo:itemList){
			if((ItemVo.getTypeId()!=null) && (ItemVo.getTypeId()==typeId)){
				items.add(ItemVo);
			}
		}
		return AjaxResult.success().put("items", items);
	}
	
	/**
	 * 新增保存服务价格设定
	 */
	@RequiresPermissions("foxwash:wShopPrice:add")
	@Log(title = "服务价格设定", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WShopPrice wShopPrice)
	{
		wShopPrice.setUuid(UUIDUtils.getUUID());
		wShopPrice.setCreator(ShiroUtils.getUserId());
		return toAjax(wShopPriceService.insertWShopPrice(wShopPrice));
	}

	/**
	 * 修改服务价格设定
	 */
	@GetMapping("/edit/{priceId}")
	public String edit(@PathVariable("priceId") Long priceId, ModelMap mmap)
	{
		WShopPrice wShopPrice = wShopPriceService.selectWShopPriceById(priceId);
		mmap.put("wShopPrice", wShopPrice);

		WServiceType serviceDO = wServiceTypeService.selectWServiceTypeById(wShopPrice.getTypeId());
		List<WServiceType> serviceList = wServiceTypeService.selectWServiceTypeList(null);
		WServiceType s = new WServiceType();
		if(serviceDO!=null && serviceDO.getTypeId()!=null && serviceList!=null){
			for(WServiceType service:serviceList){
				if(service.getTypeId()==serviceDO.getTypeId()){
					s = service;
				}
			}
			serviceList.remove(s);
		}
		mmap.addAttribute("typeList",serviceList);
		mmap.addAttribute("type",serviceDO);

		WServiceItem serviceItemDO = wServiceItemService.selectWServiceItemById(wShopPrice.getItemId());
		List<WServiceItem> itemList = null;
		if(serviceDO==null){
			itemList = wServiceItemService.selectWServiceItemList(null);
		}else{
			WServiceItem wServiceItem = new WServiceItem();
			wServiceItem.setTypeId(serviceDO.getTypeId());
			itemList = wServiceItemService.selectWServiceItemList(wServiceItem);
		}
		WServiceItem s1 = new WServiceItem();
		if(serviceItemDO!=null && serviceItemDO.getItemId()!=null && itemList!=null){
			for(WServiceItem item:itemList){
				if(item.getItemId()==serviceItemDO.getItemId()){
					s1 = item;
				}
			}
			serviceList.remove(s1);
		}
		mmap.addAttribute("itemList",itemList);
		mmap.addAttribute("item",serviceItemDO);

		WShop shopDO = wShopService.selectWShopById(wShopPrice.getShopId());
		List<WShop> shopList = null;
		shopList = wShopService.selectWShopList(null);
		WShop s2 = new WShop();
		if(shopDO!=null && shopDO.getShopId()!=null && shopList!=null){
			for(WShop wshop:shopList){
				if(wshop.getShopId()==shopDO.getShopId()){
					s2 = wshop;
				}
			}
			shopList.remove(s2);
		}
		mmap.addAttribute("shopList",shopList);
		mmap.addAttribute("shop",shopDO);

	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存服务价格设定
	 */
	@RequiresPermissions("foxwash:wShopPrice:edit")
	@Log(title = "服务价格设定", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WShopPrice wShopPrice)
	{
		wShopPrice.setModifier(ShiroUtils.getUserId());
		return toAjax(wShopPriceService.updateWShopPrice(wShopPrice));
	}
	
	/**
	 * 删除服务价格设定
	 */
	@RequiresPermissions("foxwash:wShopPrice:remove")
	@Log(title = "服务价格设定", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wShopPriceService.deleteWShopPriceByIds(ids));
	}

	/**
	 * 状态修改
	 */
	@Log(title = "服务价格管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("foxwash:wShopPrice:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(WShopPrice wShopPrice)
	{
		return toAjax(wShopPriceService.changeStatus(wShopPrice));
	}
}
